// pages/suppInformation/suppInformation.js
import api from "../../utils/api";

Page({

  /**
   * 页面的初始数据
   */
  data: {
    deptName: '',
    userName: '',
    code: '',
    id: '',
    from: '',
    cctualInvoiceNo: '', //实际发票编号
    documentNumber: '', //凭证编号
    documentDate: '', //日期
    preparer: '', //制单人
    show: false,
    minDate: '',
    maxDate: '',
    //到款项id
    receiveId:'',
    dkClaimId:''
  },
  
  //日期弹框
  onDisplay() {
    this.setData({
      minDate: new Date(2018, 0, 1).getTime(),
      maxDate: new Date(2028, 0, 31).getTime(),
      show: true
    });
  },
  onClose() {
    this.setData({
      show: false
    });
  },
  formatDate(date) {
    //转时间戳
    date = new Date(date);
    return `${date.getFullYear()}-${date.getMonth() + 1}-${date.getDate()}`;
  },
  onConfirm(event) {
    this.setData({
      show: false,
      documentDate: this.formatDate(event.detail),
    });
  },
  oninvOnChange(e) {
    console.log(e.detail, "输入的实际发票编号")
    this.setData({
      cctualInvoiceNo: e.detail
    })
  },
  onNumChange(e) {
    console.log(e, "onNumChange")
    this.setData({
      documentNumber: e.detail
    })
  },
  onDateChange(e) {
    console.log(e, "onDateChange")
    this.setData({
      documentDate: e.detail
    })
  },
  onpreChange(e) {
    console.log(e, "onpreChange")
    this.setData({
      preparer: e.detail
    })
  },


  //保存
  save() {
    let that = this;
    if (this.data.from == 'zffp') {
      //发票作废--保存
      api.fpzfbuchong({
        method: 'post',
        data: {
          id: this.data.id,
          invCode: this.data.cctualInvoiceNo,
          voucherCode: this.data.documentNumber,
          voucherDate: this.data.documentDate,
          voucherOp: this.data.preparer,
         
          
           //到款项id
        receiveId: this.data.receiveId,
        dkClaimId: this.data.dkClaimId,
        },
        success: function (res) {
          console.log(res.data.data, "发票作废--补充信息")
          if (res.data.code == 200) {
            wx.showToast({
              title: '保存成功',
              icon: 'success'
            })
            wx.navigateBack({
              delta: 1,
            })
          }
        }
      })
    } else {
      console.log(this.data.cctualInvoiceNo,'2222')
      //验证用户所填写的实际发票编号是否 为八位数字 
      let reg=/^\d{8}$/
      let jiaoyan = reg.test(this.data.cctualInvoiceNo)
      // if(jiaoyan){
      // //  console.log('是')
      // api.fpbuchong({
      //   method: 'post',
      //   data: {
      //     id: this.data.id,
      //     invCode: this.data.cctualInvoiceNo,
      //     voucherCode: this.data.documentNumber,
      //     voucherDate: this.data.documentDate,
      //     voucherOp: this.data.preparer,
      //   },
      //   success: function (res) {
      //     console.log(res.data.data, "fpbuchong")
      //     if (res.data.code == 200) {
      //       wx.showToast({
      //         title: '保存成功',
      //         icon: 'success'
      //       })
      //     }
      //   }
      // })
      // }else{
      //   // console.log('不是八位数字请提示')
      //   wx.showModal({
      //     content: '请确认"实际发票编号"是否为八位数字',
      //     showCancel: false,
      //     confirmColor: '#0041C4'
      //   })
      // }
//发票--补充信息
      api.fpbuchong({
        method: 'post',
        data: {
          id: this.data.id,
          invCode: this.data.cctualInvoiceNo,
          voucherCode: this.data.documentNumber,
          voucherDate: this.data.documentDate,
          voucherOp: this.data.preparer,

           //到款项id
        receiveId: this.data.receiveId,
        dkClaimId: this.data.dkClaimId,
        },
        success: function (res) {
          console.log(res.data.data, "fpbuchong")
          if (res.data.code == 200) {
            wx.showToast({
              title: '保存成功',
              icon: 'success'
            })
            wx.navigateBack({
              delta: 1,
            })
          }
        }
      })
     
    }

  },
  cancel() {
    wx.navigateBack({
      delta: 1,
    })
  },
  /**
   * 生命周期函数--监听页面加载
   */
  onLoad: function (options) {
    console.log(options, "1111")
    this.setData({
      code: options.code,
      deptName: options.deptName,
      userName: options.userName,
      id: options.id,
      from: options.from,
      receiveId:options.receiveId,
      cctualInvoiceNo:options.invCode =='null' ? '' : options.invCode,
      documentNumber:options.voucherCode =='null' ? '' : options.voucherCode,
      documentDate:options.voucherDate =='null' ? '' : options.voucherDate,
      preparer:options.voucherOp =='null' ? '' : options.voucherOp,
    })
  },

  /**
   * 生命周期函数--监听页面初次渲染完成
   */
  onReady: function () {

  },

  /**
   * 生命周期函数--监听页面显示
   */
  onShow: function () {
      
  },

  /**
   * 生命周期函数--监听页面隐藏
   */
  onHide: function () {

  },

  /**
   * 生命周期函数--监听页面卸载
   */
  onUnload: function () {

  },

  /**
   * 页面相关事件处理函数--监听用户下拉动作
   */
  onPullDownRefresh: function () {

  },

  /**
   * 页面上拉触底事件的处理函数
   */
  onReachBottom: function () {

  },

  /**
   * 用户点击右上角分享
   */
  onShareAppMessage: function () {

  }
})